package com.mycompany.api;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.util.*;

public class MainActivity extends Activity 
{
	private Button send_btn,addpara_btn,remove_btn,hadd_bth,hremove_btn;
	private EditText url_edit;
	private Spinner method_spinner;
	private FormLayout paraform,hparaform;
	private Map<String, List<String>> re_map;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		final Handler handler=new Handler(){
			public void handleMessage(Message msg){
				if(msg.what==0){
					//Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
					Intent intent=new Intent(MainActivity.this,Re_act.class);
					Bundle bundle=new Bundle();
					bundle.putString("data",msg.obj.toString());
					for(String key:re_map.keySet()){
						bundle.putString(key,re_map.get(key).toString());
					}
					intent.putExtras(bundle);
					startActivity(intent);
				}
			}
		};
		send_btn=findViewById(R.id.send);
		url_edit=findViewById(R.id.url_edit);
		method_spinner=findViewById(R.id.method);
		hremove_btn=findViewById(R.id.hremove_btn);
		addpara_btn=findViewById(R.id.addpara_btn);
		paraform=findViewById(R.id.paraform_layout);
		hparaform=findViewById(R.id.head_layout);
		remove_btn=findViewById(R.id.remove_btn);
		send_btn.setWidth(getWindowManager().getDefaultDisplay().getWidth());
		hadd_bth=findViewById(R.id.haddpara_btn);
		String[]items={"GET","POST"};
		ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,items);
		method_spinner.setAdapter(ad);
		ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  
		NetworkInfo ni = cm.getActiveNetworkInfo(); 
		if(ni != null && ni.isConnected())
		{
      		 Toast.makeText(this, "网络连接正常", 1000).show();
		} 
		else{
			Toast.makeText(this, "网络连接失败", 3000).show();
		}
		addpara_btn.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					
					LayoutInflater inflater=getLayoutInflater();
					View v=inflater.inflate(R.layout.paraitem_layout,null);
					paraform.addView(v);
				}
			});
		hadd_bth.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View p1)
				{

					LayoutInflater inflater=getLayoutInflater();
					View v=inflater.inflate(R.layout.paraitem_layout,null);
					hparaform.addView(v);
				}
			});
		send_btn.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					String url=url_edit.getText().toString().trim();
					if (!url.startsWith("http://")&&!url.startsWith("https://")) {
						url="http://"+url;
					}
					//判断是否带参数
					if (url.contains("?")) {
						String paras=url.substring(url.indexOf("?")+1);
						String para[]=paras.split("&");
						for (int i = 0; i < para.length; i++) {
							String[]p=para[i].split("=");
							System.out.println(p[0]+":"+p[1]);
						}
					}
					//url=Http.sendGet(url,paraform.getdata(),hparaform.getdata());
					new Thread(new Runnable(){

							@Override
							public void run()
							{
								Http http=new Http();
								Message msg=Message.obtain();
								msg.what=0;
								if(method_spinner.getSelectedItem().toString().equals("GET")){
									msg.obj=http.sendGet(url_edit.getText().toString(),paraform.getdata(),hparaform.getdata());
								}else{
									msg.obj=http.sendPost(url_edit.getText().toString(),paraform.getdata(),hparaform.getdata());
								}
								re_map=http.map;
								handler.sendMessage(msg);
							}

						
					}).start();
					Toast.makeText(MainActivity.this,url,Toast.LENGTH_SHORT).show();
				}
		});
		remove_btn.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(paraform.getChildCount()<2){
						return;
					}
					paraform.removeViewAt(paraform.getChildCount()-1);
				}
		});
		hremove_btn.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(hparaform.getChildCount()<2){
						return;
					}
					hparaform.removeViewAt(hparaform.getChildCount()-1);
				}
			});
    }
}
